package com.nf28.removies.dto;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
 
public class Film implements Serializable{
	public static class GpsCoord implements Serializable{
		private float lat;
		private float lon;
		
		public float getLat() {
			return lat;
		}
		
		public float getLon() {
			return lon;
		}
		
		public GpsCoord(float lat, float lon) {
			super();
			this.lat = lat;
			this.lon = lon;
		}

		@Override
		public String toString() {
			return "GpsCoord [lat=" + lat + ", lon=" + lon + "]";
		}
	}
 
	private int id;
	private String title;
	private byte[] cover;
	private Bitmap cachecover;
	private int year;
	private String genre;
	private Date viewdate;
	private GpsCoord viewplace;
	private boolean favori;
	private String summary;
	
	public Film(int id, String title, byte[] cover, int year, String genre,
			Date viewdate, GpsCoord viewplace, boolean favori, String summary) {
		super();
		this.id = id;
		this.title = title;
		this.cover = cover;
		this.year = year;
		this.genre = genre;
		this.viewdate = viewdate;
		this.viewplace = viewplace;
		this.favori = favori;
		this.summary = summary;
	}
	
	public Film(int id, String title, InputStream cover, int year, String genre,
			Date viewdate, GpsCoord viewplace, boolean favori, String summary) {
		super();
		this.id = id;
		this.title = title;
		setCover(cover);
		this.year = year;
		this.genre = genre;
		this.viewdate = viewdate;
		this.viewplace = viewplace;
		this.favori = favori;
		this.summary = summary;
	}
	
	public Film(){	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	public byte[] getCover() {
		return cover;
	}
	
	public Bitmap getCoverBitmap() {
		if(cachecover == null){
			cachecover = BitmapFactory.decodeStream(new ByteArrayInputStream(cover));
		}
		return cachecover;
	}
	
	//getAssets().open("images/default_cover.png")
	public void setCover(InputStream inputStream) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] tmp = new byte[4096];
		int ret = 0;

		try {
			while((ret = inputStream.read(tmp)) > 0)
			{
			    bos.write(tmp, 0, ret);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		cover = bos.toByteArray();
	}
	
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public String getGenre() {
		return genre;
	}
	public void setGenre(String genre) {
		this.genre = genre;
	}
	public Date getViewdate() {
		return viewdate;
	}
	public void setViewdate(Date viewdate) {
		this.viewdate = viewdate;
	}
	public GpsCoord getViewplace() {
		return viewplace;
	}
	public void setViewplace(GpsCoord viewplace) {
		this.viewplace = viewplace;
	}
	public boolean isFavori() {
		return favori;
	}
	public void setFavori(boolean favori) {
		this.favori = favori;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}
	
	public String getSummary() {
		return summary;
	}

	
	@Override
	public String toString() {
		return "Film [id=" + id + ", title=" + title + ", cover="
				+ cover.length
				+ ", year=" + year + ", genre=" + genre + ", viewdate="
				+ viewdate + ", viewplace=" + viewplace + ", favori=" + favori
				+ ", summary=" + summary + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Film other = (Film) obj;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}
 
}